﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;
using System.Text;

namespace HRProject.Code.DAO
{
    public class AuditDAO : BaseDAO
    {
        public AuditDAO()
            : base ()
        {
        }

        public List<BLAudit> GetAll()
        {
            List<BLAudit> result = new List<BLAudit>();
            List<BaseCondition> condition = new List<BaseCondition>();
            DataTable data = select("usp_GetAllAudits", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                int auditId = (int)data.Rows[i]["AuditTypeId"];
                string feature = (string)data.Rows[i]["Feature"];
                string featureName = (string)data.Rows[i]["FeatureName"];

                BLAudit audit = null;
                for (int j = 0; j < result.Count; j++)
                {
                    if (result[j].Key.Equals(feature))
                    {
                        audit = result[j];
                        break;
                    }
                }
                if (audit == null)
                {
                    audit = new BLAudit();
                    audit.Id = auditId;
                    audit.Key = feature;
                    audit.Name = featureName;
                    audit.Modes = new List<BLAuditMode>();
                    result.Add(audit);
                }

                BLAuditMode mode = new BLAuditMode();
                mode.Id = 0;
                mode.ModeKey = (string)data.Rows[i]["Mode"];
                mode.ModeName = (string)data.Rows[i]["ModeName"];

                audit.Modes.Add(mode);
            }
            return result;
        }

        public List<BLAudit> Search(Guid userId)
        {
            List<BLAudit> result = new List<BLAudit>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@userId", userId, ParameterDirection.Input));
            DataTable data = select("usp_SearchAuditsOfUser", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                int auditId = (int)data.Rows[i]["AuditTypeId"];
                string feature = (string)data.Rows[i]["Feature"];
                string featureName = (string)data.Rows[i]["FeatureName"];

                BLAudit audit = null;
                for (int j = 0; j < result.Count; j++)
                {
                    if (result[j].Key.Equals(feature))
                    {
                        audit = result[j];
                        break;
                    }
                }
                if (audit == null)
                {
                    audit = new BLAudit();
                    audit.Id = auditId;
                    audit.Key = feature;
                    audit.Name = featureName;
                    audit.Modes = new List<BLAuditMode>();
                    result.Add(audit);
                }

                BLAuditMode mode = new BLAuditMode();
                mode.Id = (int)data.Rows[i]["AuditId"];
                mode.ModeKey = (string)data.Rows[i]["Mode"];
                mode.ModeName = (string)data.Rows[i]["ModeName"];
                mode.IsAllowed = (mode.Id != 0);

                audit.Modes.Add(mode);
            }
            return result;
        }

        public BLAudit Get(Guid userId, string audit)
        {
            BLAudit result = new BLAudit();
            result.Key = audit;
            result.Modes = new List<BLAuditMode>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@userId", userId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@feature", audit, ParameterDirection.Input));
            DataTable data = select("usp_GetAuditOfUser", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLAuditMode mode = new BLAuditMode();
                mode.ModeKey = (string)data.Rows[i]["Mode"];
                mode.ModeName = (string)data.Rows[i]["ModeName"];
                result.Modes.Add(mode);
            }
            return result;
        }

        public int UpdateAudit(Guid userId, string audit, string modes, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@userId", userId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@feature", audit, ParameterDirection.Input));
            condition.Add(new BaseCondition("@modes", modes, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", new long(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateAudit", condition);
            errorCode = (int)condition[condition.Count-1].ParamValue;
            return result;
        }
    }
}
